package cc.z8g.browser.utils;

import androidx.annotation.NonNull;

import java.util.Locale;
import java.util.Map;

/**
 * HttpUtils
 *
 * @author zhaoxuyang.net
 * @since 2024-08-27
 */
public final class HttpUtils {

    public static String getHeaderField(@NonNull Map<String, String> headers, @NonNull String key) {
        for (Map.Entry<String, String> kv : headers.entrySet()) {
            if (key.equalsIgnoreCase(kv.getKey())) {
                return kv.getValue();
            }
        }
        return "";
    }

    public static String formatSize(int bytes) {
        if (bytes <= 0) {
            return "";
        }
        if (bytes < 1024) {
            return bytes + " B";
        }
        if (bytes <= 1024 * 1024) {
            return String.format(Locale.getDefault(), "%.2f K", bytes / 1024F);
        }
        if (bytes <= 1024 * 1024 * 1024) {
            return String.format(Locale.getDefault(), "%.0f M", bytes / 1024F / 1024F);
        }
        return String.format(Locale.getDefault(), "%.0f G", bytes / 1024F / 1024F / 1024F);
    }
}
